//
//  LCCustomerActionSheet.m
//  lcs_c
//
//  Created by chaiyanan on 15/10/19.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "LCCustomerActionSheet.h"

@interface LCCustomerActionSheet ()

@property (nonatomic, strong) void(^CancelAction)();
@property (nonatomic, strong) NSArray *otherButtonTitlesAry;
@property (nonatomic, strong) void(^OtherAction)(NSInteger index,LCCustomerActionSheet *actionSheet);
@property (nonatomic, strong) UIControl *bgView;
@property (nonatomic, strong) UIView *popView;
@property (nonatomic) BOOL isShow;
@property (nonatomic) BOOL isActive;



@end

@implementation LCCustomerActionSheet

static LCCustomerActionSheet *sharedLCCustomerActionSheet = nil;

+ (LCCustomerActionSheet *)sharedLCCustomerActionSheet
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedLCCustomerActionSheet = [[LCCustomerActionSheet alloc]init];
        sharedLCCustomerActionSheet.isShow = NO;
        sharedLCCustomerActionSheet.isActive = NO;
    });
    return sharedLCCustomerActionSheet;
}


- (instancetype)initWithTitle:(NSString *)title
            cancelButtonTitle:(NSString *)cancelButtonTitle
           cancelButtonAction:(void (^)())CancelAction
            otherButtonTitles:(NSArray *)otherButtonTitlesAry
            otherButtonAction:(void (^)(NSInteger,LCCustomerActionSheet *))OtherAction
{
//    self = [[LCCustomerActionSheet alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT)];
    
    if (self.isShow)
    {
        return self;
    }
    self.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
    self.isShow = YES;
    
    self.CancelAction = CancelAction;
    self.otherButtonTitlesAry = otherButtonTitlesAry;
    self.OtherAction = OtherAction;
    
    self.bgView = [[UIControl alloc]initWithFrame:self.bounds];
    self.bgView.alpha = 0;
    [self addSubview:self.bgView];
    [self.bgView addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchDown];
    
    UIView *popView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    CGRect popFrame = popView.frame;
    if (cancelButtonTitle&&title)
    {
        popFrame.size.height = 50 + 6 + 50 + 6 + otherButtonTitlesAry.count * 50;
        popFrame.origin.y = SCREEN_HEIGHT - popFrame.size.height;
    }
    else
    {
        if (cancelButtonTitle || title)
        {
            popFrame.size.height = 50 + 6 + otherButtonTitlesAry.count * 50;
            popFrame.origin.y = SCREEN_HEIGHT - popFrame.size.height;
        }
        else
        {
            popFrame.size.height = otherButtonTitlesAry.count * 50;
            popFrame.origin.y = SCREEN_HEIGHT - popFrame.size.height;
        }
    }
    popView.frame = popFrame;
    [self addSubview:popView];
    self.popView = popView;
    
    if (cancelButtonTitle)
    {
        UIButton *cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.popView.bounds) - 50, SCREEN_WIDTH, 50)];
        [cancelButton setTitle:cancelButtonTitle forState:UIControlStateNormal];
        [cancelButton setTitleColor:[LCCommon getColor:@"000000"] forState:UIControlStateNormal];
        [cancelButton.titleLabel setFont:[UIFont systemFontOfSize:17.0f]];
        [cancelButton setBackgroundColor:[UIColor whiteColor]];
        [cancelButton addTarget:self action:@selector(cancelBtnTap) forControlEvents:UIControlEventTouchUpInside];
        
        UIView *separatorView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.popView.bounds) - 56, SCREEN_WIDTH, 6)];
        separatorView.backgroundColor = [LCCommon getColor:@"e7e7ee"];
        
        [popView addSubview:cancelButton];
        [popView addSubview:separatorView];
    }
    
    for (int i=0; i < otherButtonTitlesAry.count; i++)
    {
        NSString *otherTitle = otherButtonTitlesAry[i];
        UIButton *otherBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
        otherBtn.tag = i;
        [otherBtn setTitle:otherTitle forState:UIControlStateNormal];
        [otherBtn setTitleColor:[LCCommon getColor:@"000000"] forState:UIControlStateNormal];
        [otherBtn.titleLabel setFont:[UIFont systemFontOfSize:17.0f]];
        [otherBtn setBackgroundColor:[UIColor whiteColor]];
        [otherBtn addTarget:self action:@selector(otherButtonTap:) forControlEvents:UIControlEventTouchUpInside];
        
        CGRect frame = otherBtn.frame;
        if (cancelButtonTitle)
        {
            frame.origin.y = CGRectGetHeight(self.popView.bounds) - ( (otherButtonTitlesAry.count - i) * 50 + 56);
            
        }
        else
        {
            frame.origin.y = CGRectGetHeight(self.popView.bounds) - (otherButtonTitlesAry.count - i) * 50;
        }
        otherBtn.frame = frame;
        
        [popView addSubview:otherBtn];
        
        if (i != otherButtonTitlesAry.count -1)
        {
            UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(frame)-.5, SCREEN_WIDTH, .5)];
            line.backgroundColor = [LCCommon getColor:@"dfdfdf"];
            [popView addSubview:line];
        }
        
    }
    
    if(title)
    {
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
        lable.text = title;
        UIView *separatorView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 6)];
        
        CGRect frame = lable.frame;
        CGRect separatorFrame = separatorView.frame;
        if (cancelButtonTitle)
        {
            frame.origin.y = CGRectGetHeight(self.popView.bounds) - ( otherButtonTitlesAry.count * 50 + 56)-56;
            separatorFrame.origin.y = CGRectGetHeight(self.popView.bounds) - ( otherButtonTitlesAry.count * 50 + 56)-6;
            
        }
        else
        {
            frame.origin.y = CGRectGetHeight(self.popView.bounds) - otherButtonTitlesAry.count * 50 - 56;
            separatorFrame.origin.y = CGRectGetHeight(self.popView.bounds) - ( otherButtonTitlesAry.count * 50 + 6 );
        }
        lable.frame = frame;
        lable.backgroundColor = [UIColor whiteColor];
        lable.font = [UIFont systemFontOfSize:17.0f];
        lable.textColor = [LCCommon getColor:@"000000"];
        lable.textAlignment = NSTextAlignmentCenter;
        [popView addSubview:lable];
        
        separatorView.backgroundColor = [LCCommon getColor:@"e7e7ee"];
        separatorView.frame = separatorFrame;
        [popView addSubview:separatorView];
    }
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationDidChange:) name:UIApplicationWillTerminateNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationDidChange:) name:UIApplicationDidEnterBackgroundNotification object:nil];
    
    return self;
}

- (void)showInView
{
    if (self.isActive)
    {
        return;
    }
    self.isActive = YES;
    [UIView animateWithDuration:0.25 animations:^{
        self.frame = CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT);
        [[UIApplication sharedApplication].keyWindow addSubview:self];
    }completion:^(BOOL finished) {
        self.bgView.backgroundColor = [UIColor blackColor];
        self.bgView.alpha = .4;

    }];
}

- (void)applicationDidChange:(NSNotification *)notification
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    [self removeFromSuperview];

}

- (void)dismiss
{
    [UIView animateWithDuration:0.25 animations:^{
        CGRect frame = _popView.frame;
        frame.origin.y = SCREEN_HEIGHT;
    }completion:^(BOOL finished) {
        [[NSNotificationCenter defaultCenter]removeObserver:self];
        [self removeFromSuperview];
        
    }];
}

- (void)removeFromSuperview
{
    [super removeFromSuperview];
    [self removeSubviews];
}

- (void)removeSubviews
{
    for (id obj in self.subviews)
    {
        [obj removeFromSuperview];
    }
    self.isShow = NO;
    self.isActive = NO;
    self.CancelAction = nil;
    self.otherButtonTitlesAry = nil;
    self.OtherAction = nil;
    self.bgView = nil;
    self.popView = nil;
    
}

- (void)cancelBtnTap
{
    if (self.CancelAction)
    {
        self.CancelAction();
    }
    [self dismiss];
}

- (void)otherButtonTap:(UIButton *)sender
{
    self.OtherAction(sender.tag,self);
}

@end
